package com.berry.MedCheck.db;


import android.app.Activity;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import com.berry.MedCheck.R;

/**
 * Created with IntelliJ IDEA.
 * User: Airay.Rong
 * Date: 12-6-28
 * Time: 下午3:11
 * To change this template use File | Settings | File Templates.
 */
public class DBOperatorTest extends Activity implements View.OnClickListener {
	private static final String TAG = "DBOperatorTest";

	private Button btnAdd;
	private Button btnDelAll;
	private ListView mList;
	private Cursor mCursor;
	private Uri mUri;
	private SimpleCursorAdapter mAdapter;


	private static final String[] PROJECTION = new String[]{
			UserDB.DBColumns._ID, // 0
			UserDB.DBColumns.USERNAME, // 2
			UserDB.DBColumns.SEX, // 3
			UserDB.DBColumns.AGE, // 4
			UserDB.DBColumns.HEIGHT, // 5
			UserDB.DBColumns.WEIGHT, // 6
	};
	private static final int[] viewIDs = {
			R.id.dbid,
			R.id.name,
			R.id.sex,
			R.id.age,
			R.id.height,
			R.id.weight
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dbtest_activity);
		btnAdd = (Button) findViewById(R.id.btnDBAdd);
		btnDelAll = (Button) findViewById(R.id.btnDBDelAll);

		btnAdd.setOnClickListener(this);
		btnDelAll.setOnClickListener(this);
		mList = (ListView) findViewById(R.id.list);
		Intent intent = getIntent();
		if (intent.getData() == null) {
			intent.setData(UserDB.DBColumns.CONTENT_URI);
		}
		mCursor = managedQuery(
				getIntent().getData(),            // Use the default content URI for the provider.
				PROJECTION,                       // Return the note ID and title for each note.
				null,                             // No where clause, return all records.
				null,                             // No where clause, therefore no where column values.
				UserDB.DBColumns.DEFAULT_SORT_ORDER  // Use the default sort order.
		);

		mAdapter = new SimpleCursorAdapter(this, R.layout.user_full_info_item, mCursor, PROJECTION, viewIDs);
		mList.setAdapter(mAdapter);

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.btnDBAdd:
				save();
				mAdapter.notifyDataSetChanged();
				break;
			case R.id.btnDBDelAll:
				deleteAll();
				break;
		}

	}

	private final void save() {
		// Make sure their current
		// changes are safely saved away in the provider.  We don't need
		// to do this if only editing.
		mUri = getContentResolver().insert(UserDB.DBColumns.CONTENT_URI, null);
		mCursor = managedQuery(mUri, PROJECTION, null, null, null);
		if (mCursor != null) {
			// Get out updates into the provider.
			ContentValues values = new ContentValues();

			// Bump the modification time to now.

			String text = "123";//+Math.random();

			values.put(UserDB.DBColumns.USERNAME, text);
			values.put(UserDB.DBColumns.SEX, 0);
			values.put(UserDB.DBColumns.AGE, 10);
			values.put(UserDB.DBColumns.HEIGHT, 150);
			values.put(UserDB.DBColumns.WEIGHT, 60);

			// Write our text back into the provider.
//            values.put(UserDB.DBColumns.NOTE, text);

			// Commit all of our changes to persistent storage. When the update completes
			// the content provider will notify the cursor of the change, which will
			// cause the UI to be updated.
			try {
				getContentResolver().update(mUri, values, null, null);
			} catch (NullPointerException e) {
				Log.e(TAG, e.getMessage());
			}

		}
	}

	private final void deleteAll() {
		getContentResolver().delete(UserDB.DBColumns.CONTENT_URI, null, null);
	}

	private void enableSMSReceiver() {

		final PackageManager pm = this.getPackageManager();
		final ComponentName componentName = new ComponentName("hi.com.baby",
				"hi.com.baby.BabySMSReceiver");
		pm.setComponentEnabledSetting(componentName,
				PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
				PackageManager.DONT_KILL_APP);

	}
}